<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true"
          >添加分类</el-button
        >
      </div>
      <!-- 表格区域 -->
      <!-- type="index" 可以自定义索引,返回值将作为索引展示 -->
      <!-- stripe 属性可以创建带斑马纹的表格 -->
      <!-- size="mini" 设置按钮大小 -->
      <el-table style="width: 100%" :data="cateList" border stripe>
        <el-table-column
          label="序号"
          type="index"
          width="100"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <!-- 使用作用域插槽 设置表单的属性 -->
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="showEdit(scope.row)"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="del(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      @closed="$refs.addForm.resetFields()"
      title="添加文章分类"
      :visible.sync="addVisible"
      width="35%"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        label-width="70px"
        :rules="addRules"
        class="demo-addForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      @closed="$refs.editForm.resetFields()"
      title="修改文章分类"
      :visible.sync="editVisible"
      width="35%"
    >
      <el-form
        ref="editForm"
        :model="editForm"
        label-width="70px"
        :rules="addRules"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="editCate"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  created() {
    // 使用钩子函数获取 data 里的数据
    this.getCateList()
  },
  data() {
    return {
      cateList: [],
      addVisible: false,
      // 添加表单的数据对象
      addForm: { cate_name: '', cate_alias: '' },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          {
            required: true,
            message: '请输入分类别名',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      },
      editForm: { cate_name: '', cate_alias: '' },
      editVisible: false
    }
  },
  methods: {
    // 使用 axios 获取后端数据,放入数组中
    async getCateList() {
      // 发请求获取分类列表
      const { data: res } = await this.$http.get('/my/cate/list')
      //   console.log(res)
      if (res.code === 0) {
        this.cateList = res.data
      }
    },
    // 点击确定按钮,判断表单的校验
    addCate() {
      // 1. 兜底校验
      this.$refs.addForm.validate(async (valid) => {
        if (!valid) return
        // 2. 发请求
        const { data: res } = await this.$http.post(
          '/my/cate/add',
          this.addForm
        )
        // 3.根据结果提醒用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 4.关闭 dialog
        this.addVisible = false
        // 5.重新获取分类列表
        this.getCateList()
      })
    },
    // 点击操作修改按钮
    showEdit(row) {
      // scope 不能用插值表达式渲染,$route 也不行,如果需要看他们的内部成员,可以在控制台打印
      //   console.log(row)
      // 1.让编辑框显示
      this.editVisible = true
      // 2.渲染当前点击的数据项
      this.editForm = { ...row }
    },
    //
    editCate() {
      // 1.表单验证
      this.$refs.editForm.validate(async (valid) => {
        if (!valid) return
        // 2.发起请求
        const { data: res } = await this.$http.put(
          '/my/cate/info',
          this.editForm
        )
        // 3.修改失败
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 4.关闭 dialog
        this.editVisible = false
        // 5.重新获取分类列表
        this.getCateList()
      })
    },
    // 操作删除按钮处理函数
    del(id) {
      this.$confirm('此操作将永久删除该分类,是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 用户点击了确认
          // 1.发请求(根据 id 删除数据)
          const { data: res } = await this.$http.delete('/my/cate/del', {
            params: {
              id
            }
          })
          // 2.根据结果提醒用户
          if (res.code !== 0) return this.$message.error(res.message)
          this.$message.success(res.message)
          // 3.重新获取分类列表
          this.getCateList()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
